package com.shuqi.model.sharedprefs;

import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.SharedPreferenceUtil;
import com.shuqi.model.net.UrlManager;

/**
 * 从服务器获取的动态接口域名：保存，获取
 * @author vip
 *
 */
public class ServerDomains {
    private final String logTag = "ServerDomains";

    private static ServerDomains staticDomains;
    private ServerDomains(){}
    public static ServerDomains getInstance() {
        if (staticDomains == null) {
            staticDomains = new ServerDomains();
        }
        return staticDomains;
    }


    /***
     * 软件的服务器列表的保存
     * 
     * @param groupList
     * @param context
     */
    public boolean saveServerList(List<List<String>> groupList, Context context) {
        boolean result = false;
        SharedPreferences sp =
                context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_SERVERS,
                        Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        if (groupList != null && groupList.size() != 0) {
            //  读 , 写 ,账户,搜索,支付,info
            //【第一组】[默认]
            // 000,001,002,003,004,005  默认
            // 010,011,012,013,014,015  重试1
            // 020,021,022,023,024,025  重试2
            // 【第二组】
            // 100,101,102,103,104,105
            // 110,111,112,113,114,115
            // 120,121,122,123,124,125
            // 【第三组】
            // 200,201,202,203,204,205
            // 210,211,212,213,214,215
            // 220,221,222,223,224,225

            int num = 0;
            for (int i = 0; i < groupList.size(); i++) {
                for (int j = 0; j < groupList.get(i).size(); j++) {
                    String[] ips = groupList.get(i).get(j).trim().split("_");
                    for (int k = 0; k < ips.length; k++) {
                        editor.putString("" + i + j + k, ips[k]);
                        num++;
                    }
                }
            }
            boolean saveGroup = editor.commit();
            Log4an.e(logTag, "软件重试域名列表：保存完成:" + saveGroup +",条数="+num);

            // 获取用户选择的备选服务器序列号
            int serverIndex = sp.getInt("server_index", 0);
            // 没存保存序号时 || 有序号,但超过了服务器组范围
            if (serverIndex < 0 || serverIndex >= groupList.size()) {
                serverIndex = 0;
            }
            // 存储当前序号的服务器组
            editor.putInt("server_index", serverIndex);

            // 首次连网
            editor.putString("standby_server0_0", sp.getString(serverIndex + "00", null));// 读域名
            editor.putString("standby_server0_1", sp.getString(serverIndex + "01", null));// 写域名
            editor.putString("standby_server0_2", sp.getString(serverIndex + "02", null));// 账号域名
            editor.putString("standby_server0_3", sp.getString(serverIndex + "03", null));// 搜索域名
            editor.putString("standby_server0_4", sp.getString(serverIndex + "04", null));// 充值域名
            editor.putString("standby_server0_5", sp.getString(serverIndex + "05", null));// appInfo域名
            // 重试1
            editor.putString("standby_server1_0", sp.getString(serverIndex + "10", null));
            editor.putString("standby_server1_1", sp.getString(serverIndex + "11", null));
            editor.putString("standby_server1_2", sp.getString(serverIndex + "12", null));
            editor.putString("standby_server1_3", sp.getString(serverIndex + "13", null));
            editor.putString("standby_server1_4", sp.getString(serverIndex + "14", null));
            editor.putString("standby_server1_5", sp.getString(serverIndex + "15", null));
            // 重试2
            editor.putString("standby_server2_0", sp.getString(serverIndex + "20", null));
            editor.putString("standby_server2_1", sp.getString(serverIndex + "21", null));
            editor.putString("standby_server2_2", sp.getString(serverIndex + "22", null));
            editor.putString("standby_server2_3", sp.getString(serverIndex + "23", null));
            editor.putString("standby_server2_4", sp.getString(serverIndex + "24", null));
            editor.putString("standby_server2_5", sp.getString(serverIndex + "25", null));
            boolean saveServer = editor.commit();
            Log4an.e(logTag, "当前使用的重试域名：保存完成:" + saveServer);
            result = saveGroup && saveServer;
        }
        return result;
    }
    
    /**
     * 获取请求域名(0:默认，1:重试一，2:重试二)
     * @param context
     * @param type
     * @return
     */
    public String[] getDomainsByType(Context context,String[] urls, int type){
        if(urls == null){
            urls = new String[3];
        }
        SharedPreferences sp = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_SERVERS, Context.MODE_PRIVATE);
        for (int i = 0; i < 3; i++) {
            urls[i] = sp.getString("standby_server"+ i +"_"+type, UrlManager.SERVER_IPS[0][i][type]).trim();
            Log4an.i(logTag, i+"_"+urls[i]);
        }
        return urls;
    }

}
